//
//  CustomDayDatePicker.m
//  基本框架
//
//  Created by runone02 on 2018/7/24.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "CustomDayDatePicker.h"

@interface CustomDayDatePicker ()
/** */
@property (nonatomic, strong) NSArray *dayArray1;
/** */
@property (nonatomic, strong) NSArray *dayArray2;
/** 年*/
@property (nonatomic, strong) NSMutableArray *yearArray;
/**选择框 */
@property (nonatomic, strong) UIPickerView *picker;

@end

@implementation CustomDayDatePicker

@synthesize year,month,day;
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.picker = [[UIPickerView alloc] initWithFrame:self.bounds];
        self.picker.delegate = self;
        self.picker.dataSource = self;
        self.picker.showsSelectionIndicator = YES;
        [self addSubview:self.picker];
        
        self.dayArray1 = [NSArray arrayWithObjects:@"31",@"28",@"31",@"30",@"31",@"30",@"31",@"31",@"30",@"31",@"30",@"31", nil];
        self.dayArray2 = [NSArray arrayWithObjects:@"31",@"29",@"31",@"30",@"31",@"30",@"31",@"31",@"30",@"31",@"30",@"31", nil];
        
        self.yearArray = [[NSMutableArray alloc] initWithCapacity:0];
        NSDate *date = [NSDate date];
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        
        //年
        [formatter setDateFormat:@"yyyy"];
        year = [[formatter stringFromDate:date] intValue];
        
        //月
//        [formatter setDateFormat:@"MM"];
//        month = [[formatter stringFromDate:date] intValue];
        
        //日
//        [formatter setDateFormat:@"dd"];
//        day = [[formatter stringFromDate:date] intValue];
        
        for (int i = year-30; i<year+30; i++) {
            NSString *str = [NSString stringWithFormat:@"%d",i];
            [self.yearArray addObject:str];
        }
        [self.picker selectRow:30 inComponent:0 animated:YES];
        [self.picker selectRow:self.dayArray1.count inComponent:0 animated:YES];
        [self.picker selectRow:self.dayArray2.count inComponent:0 animated:YES];
    }
    return self;
}

#pragma mark - PickerView Datadelegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return self.yearArray.count;
    }else if (component == 1){
//        return 12;
        return self.dayArray1.count;
    }else{
//        //获取前二个滚轮的当前所选行的索引
        int row = (int)[self.picker selectedRowInComponent:0];
        int nowyear = [[self.yearArray objectAtIndex:row] intValue];
//        int nowmonth = (int)[self.picker selectedRowInComponent:1];
//        if ((nowyear % 4 == 0 && nowyear % 100 !=0 )||(nowyear % 400 == 0)) {
//              return self.dayArray2.count;
//        }
//        else {
//              return self.dayArray2.count;
//        }
         return self.dayArray2.count;
    }
}


//设置数据源
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    
    UILabel *label = [[UILabel alloc] init];
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    label.frame = CGRectMake(0, 0, self.frame.size.width/3.0, 50);
    [label setFont:[UIFont boldSystemFontOfSize:16]];
    if (component == 0) {
        label.text = [NSString stringWithFormat:@"%@",[self.yearArray objectAtIndex:row]];

    }else if (component == 1){
//        mycom1.text = [NSString stringWithFormat:@"%ld",row+1];
     label.text = [NSString stringWithFormat:@"%@",[self.dayArray1 objectAtIndex:row]];
        month = [self.dayArray1 objectAtIndex:row];
    }else{
//        mycom1.text = [NSString stringWithFormat:@"%ld",row+1];
         label.text = [NSString stringWithFormat:@"%@",[self.dayArray2 objectAtIndex:row]];
        //    day = (int)rowd+1;
        day = [self.dayArray2 objectAtIndex:row];
    }
    return label;
    
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    return self.frame.size.width/3.0;
}


//设置监听滚动
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    if (component == 0 || component == 1) {
        //当第一个滚轮发生变化时,刷新第二个滚轮的数据
        [self.picker reloadComponent:2];
        //让刷新后的第二个滚轮重新回到第一行
        [self.picker selectRow:0 inComponent:2 animated:YES];
    }
    int rowy = (int)[self.picker selectedRowInComponent:0];
    int rowm = (int)[self.picker selectedRowInComponent:1];
    int rowd = (int)[self.picker selectedRowInComponent:2];
    
    year = [[self.yearArray objectAtIndex:rowy] intValue];
//    month = (int)rowm+1;
    month = [self.dayArray1 objectAtIndex:rowm];
    
//    day = (int)rowd+1;
    day = [self.dayArray2 objectAtIndex:rowd];
    
}

@end
